//
//  HiContactController.m
//  HiCar
//
//  Created by 丁一 on 2019/2/14.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "HiContactController.h"
#import "HiAboutTextView.h"
#import "HiContactViewModel.h"

@interface HiContactController ()<HiContactDelegate>{
    NSInteger choosetype;
}

@property (nonatomic, strong) UIButton *ChooseButton;
@property (nonatomic, strong) UITextField *textfield;
@property (nonatomic, strong) HiAboutTextView *textView;
@property (nonatomic, strong) UIButton *DoneButton;

@end

@implementation HiContactController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.textView];
    self.view.backgroundColor=backColor;
    
    self.navigationItem.title=@"建议与反馈";
    
    choosetype = 10;
    
    if (_type!=2) {
        _ChooseButton           = [[UIButton alloc] initWithFrame:CGRectMake(15, NAV_Height+15,RECRWITCH-30, 45)];
        _ChooseButton.layer.cornerRadius = 5;
        _ChooseButton.layer.borderColor=LineColor.CGColor;
        _ChooseButton.layer.borderWidth=1.0;
        _ChooseButton.layer.masksToBounds = YES;
        [_ChooseButton setTitle:@"选择联系方式" forState:UIControlStateNormal];
        _ChooseButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _ChooseButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [_ChooseButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_ChooseButton addTarget:self action:@selector(go_choose) forControlEvents:UIControlEventTouchUpInside];
        _ChooseButton.backgroundColor = [UIColor whiteColor];
        [_ChooseButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.view addSubview:_ChooseButton];
        
        _textfield    = [[UITextField alloc] initWithFrame:CGRectMake(15, NAV_Height+70,RECRWITCH-30, 45)];
        _textfield.textColor       = [UIColor blackColor];
        _textfield.font            = [UIFont systemFontOfSize:16];
        _textfield.leftView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        _textfield.leftViewMode = UITextFieldViewModeAlways;
        [_textfield setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        _textfield.backgroundColor = [UIColor whiteColor];
        _textfield.placeholder     = @"请填写联系内容";
        self->_textfield.userInteractionEnabled = NO;
        _textfield.layer.cornerRadius = 5;
        _textfield.layer.borderColor=LineColor.CGColor;
        _textfield.layer.borderWidth=1.0;
        [self.view addSubview:_textfield];
        _textView.frame=CGRectMake(15, NAV_Height+40+85, RECRWITCH-30, RECRWITCH/1.8);
    }
    
    _DoneButton           = [[UIButton alloc] init];
    _DoneButton.layer.cornerRadius  = 6;
    _DoneButton.layer.masksToBounds = YES;
    [_DoneButton setTitle:@"提交" forState:UIControlStateNormal];
    [_DoneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
    [_DoneButton addTarget:self action:@selector(go_done) forControlEvents:UIControlEventTouchUpInside];
    [_DoneButton setBackgroundImage:[JonesClass createImageWithColor:Buttoncolor] forState:UIControlStateNormal];
    [_DoneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:_DoneButton];
    
    [_DoneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(130);
        make.top.equalTo(self.textView.mas_bottom).mas_equalTo(25);
        make.centerX.mas_equalTo(self.view);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)go_choose{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"联系方式" preferredStyle: UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"电话" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        self->choosetype = 0;
        [self->_ChooseButton setTitle:@"电话" forState:UIControlStateNormal];
        self->_textfield.placeholder     = @"请填写联系电话";
        self->_textfield.userInteractionEnabled = YES;
    }];
    UIAlertAction *mailAction = [UIAlertAction actionWithTitle:@"邮箱" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        self->choosetype = 1;
        [self->_ChooseButton setTitle:@"邮箱" forState:UIControlStateNormal];
        self->_textfield.placeholder     = @"请填写您的邮箱";
        self->_textfield.userInteractionEnabled = YES;
    }];
    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        self->choosetype = 2;
        [self->_ChooseButton setTitle:@"微信" forState:UIControlStateNormal];
        self->_textfield.placeholder     = @"请填写微信账号";
        self->_textfield.userInteractionEnabled = YES;
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:deleteAction];
    [alertController addAction:mailAction];
    [alertController addAction:archiveAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (HiAboutTextView *)textView{
    if (!_textView) {
        _textView = [HiAboutTextView show:CGRectMake(15, NAV_Height+15, RECRWITCH-30, RECRWITCH/1.8)];
        _textView.layer.cornerRadius = 5;
        _textView.layer.borderColor=LineColor.CGColor;
        _textView.layer.borderWidth=1.0;
        if (_type==0) {
            _textView.textview.placeholder = @"请输入联系我们的原因！";
        }else if (_type==1){
            _textView.textview.placeholder = @"请输入合作内容！";
        }else if (_type==2){
            _textView.textview.placeholder = @"请输入您的建议！";
        }
    }
    return _textView;
}

- (void)go_done{
    
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    
    if (_type!=2){
        if (choosetype==10) {
            [SVProgressHUD showErrorWithStatus:@"请选择联系方式"];
            return;
        }else if (_textfield.text.length<5||_textfield.text.length>20){
            [SVProgressHUD showErrorWithStatus:@"联系内容不正确"];
            return;
        }
    }
    
    if (_textView.textview.text.length<5) {
        [SVProgressHUD showErrorWithStatus:@"请填写有效的内容"];
    }

    [postdic setValue:[NSString stringWithFormat:@"%li",(long)_type] forKey:@"type"];
    if (_type!=2){
        [postdic setValue:[NSString stringWithFormat:@"%li",(long)choosetype] forKey:@"phonetype"];
        [postdic setValue:_textfield.text forKey:@"phone"];
    }else{
        [postdic setValue:@"" forKey:@"phonetype"];
        [postdic setValue:@"" forKey:@"phone"];
    }
    [postdic setValue:_textView.textview.text forKey:@"info"];
    
    HiContactViewModel *vmodel=[[HiContactViewModel alloc] init:postdic];
    vmodel.delegate=self;
}

- (void)Succes:(int)value{
    if (value==1) {
        [self goback];
    }
}

@end
